//
//  ExitView.m
//  Kids
//
//  Created by LiTengFei on 14/11/30.
//  Copyright (c) 2014年 90's Technology Co.Ltd. All rights reserved.
//

#import "ExitView.h"
@interface ExitView ()

@property (nonatomic, strong) UIButton* exitButton;
@property (nonatomic, strong) UIButton* cancelButton;

@end
@implementation ExitView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIView* view = [[UIView alloc] init];
        view.userInteractionEnabled = YES;
        
        self.exitButton = ({
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(20, 15, CGRectGetWidth(frame)-40, 47);
            [button setTitle:@"退出账号" forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"yellow4"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"yellow4_press"] forState:UIControlStateHighlighted];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
        
        self.cancelButton = ({
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(20, 80, CGRectGetWidth(frame)-40, 47);
            [button setBackgroundImage:[UIImage imageNamed:@"cancel_button"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"cancel_button_press"] forState:UIControlStateHighlighted];
            [button addTarget:self action:@selector(dissmiss) forControlEvents:UIControlEventTouchUpInside];
            button.translatesAutoresizingMaskIntoConstraints =NO;
            button;
        });
        
        [view addSubview:self.exitButton];
        [view addSubview:self.cancelButton];
        view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background2"]];
        view.frame = CGRectMake(0, CGRectGetHeight(self.frame) - 150 - 64, CGRectGetWidth(self.frame), 200);
        [self addSubview:view];
    }
    return self;
}
- (void)showFromBottomInView:(UIView*)view
{
    
    CGRect frame = view.frame;
    self.frame = CGRectMake(0, CGRectGetHeight(frame), CGRectGetWidth(frame), CGRectGetHeight(frame));
    self.alpha = 0.0;
    [view addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
        self.frame  = ((CGRect){{0,CGRectGetHeight(view.frame)-CGRectGetHeight(self.frame)},frame.size});
    } completion:^(BOOL finished) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    }];
}

- (void)dissmiss
{
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.0];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.0;
        self.frame  = CGRectMake(0, CGRectGetHeight(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)buttonClick:(UIButton*)button
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(exitView:didSelectedButtonAtIndex:)]) {
        [self.delegate exitView:self didSelectedButtonAtIndex:0];
         [self dissmiss];
    }
}

@end
